﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Web.UI.WebControls;
using CMS.Model;
using System.Collections.Generic;
using CMS.Logging;

namespace CMSWCSFSolution.CMS.Views
{
	public partial class UpdateTeacher : BasePage, IUpdateTeacherView
    {
        #region Ptotected Method
        private UpdateTeacherPresenter _presenter;
        private int _teacherid;
        [CreateNew]
        public UpdateTeacherPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
            
            _teacherid = Convert.ToInt32(Request["ID"]);
			if (!this.IsPostBack)
			{
                try
                {
                    _presenter.OnSelectedAllDepartments();
                    List<Teacher> teacherList = new List<Teacher>();
                    teacherList = _presenter.OnSelectTeacherById();
                    foreach (var teacher in teacherList)
                    {
                        this.TeacherNameBox.Text = teacher.TeacherName.Trim();
                        List<Department> depList = new List<Department>();
                        depList = _presenter.OnSelectedDepartmentById();
                        foreach (var dep in depList)
                        {
                            this.DropDownListDepartment.SelectedValue = dep.DepartmentName;
                        }
                        if (teacher.TeacherGender.Trim().Equals("Male"))
                        {
                            RadioButtonList1.SelectedIndex = 0;
                        }
                        else
                        {
                            RadioButtonList1.SelectedIndex = 1;
                        }
                    }
                }
                catch (Exception ee)
                {
                    Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                }
            }
		}
        /// <summary>
        /// Handles the Click event of the Button_Update control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Button_Update_Click(object sender, EventArgs e)
        {
            try
            {
                _presenter.OnUpdateTeacher();
                ShowMessage(1, "Update");
                
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3,"error");
            }
        }
        #endregion

        #region Public Property
        /// <summary>
        /// Sets the bind department.
        /// </summary>
        /// <value>
        /// The bind department.
        /// </value>
        public List<Department> BindDepartment
        {
            set
            {
                DropDownListDepartment.DataSource = value;
                DropDownListDepartment.Items.Clear();
                DropDownListDepartment.Items.Capacity = value.Count;
                DropDownListDepartment.Items.Insert(0, "Please Select");
                foreach (var a in value)
                {
                    ListItem b = new ListItem(a.DepartmentName);
                    if (!DropDownListDepartment.Items.Contains(b))
                    {
                        DropDownListDepartment.Items.Insert(a.DepartmentId, a.DepartmentName);
                    }
                }
            }
        }
        /// <summary>
        /// Gets the teacher update.
        /// </summary>
        public Teacher teacherUpdate
        {
            get
            {
                Teacher teacher = new Teacher(0, null, null, 0, null);
                teacher.TeacherId = _teacherid;
                teacher.TeacherName = TeacherNameBox.Text.Trim();
                teacher.TeacherGender = (RadioButtonList1.SelectedIndex == 0) ? "Male" : "Female";
                teacher.DepartmentId = DropDownListDepartment.SelectedIndex; ;
                return teacher;
            }
        }
        /// <summary>
        /// Gets the teacher id.
        /// </summary>
        public int TeacherId
        {
            get { return _teacherid; }
        }
        /// <summary>
        /// Gets the department list.
        /// </summary>
        public List<Department> departmentList
        {
            get 
            {  
              return _presenter.OnSelectedDepartmentById();
            }
        }
        /// <summary>
        /// Gets the department.
        /// </summary>
        public Department department
        {
            get { return _presenter.OnSelectedDepartmentById1(); }
        }
        #endregion
    }
}

